package bTree.LeetCode;

public class 在排序数组中查找元素的第一个和最后一个位置 {
    public int[] searchRange(int[] nums, int target) {
        return new int[]{searchLeft(nums,target),searchRight(nums,target)};

    }

    private int searchRight(int[] nums, int target) {
        int left=0,right=nums.length-1;
        int res=-1;
        while (left<=right){
            int mid = (left + right) / 2;
            int midNum = nums[mid];
            if (midNum==target){
                res=mid;
                left=mid+1;
            }else if (midNum>target){
                right=mid-1;
            }else {
                left=mid+1;
            }
        }
        return res;
    }

    private int searchLeft(int[] nums, int target) {
        int left=0,right=nums.length-1;
        int res=-1;
        while (left<=right){
            int mid = (left + right) / 2;
            int midNum = nums[mid];
            if (midNum==target){
                res=mid;
                right=mid-1;
            }else if (midNum>target){
                right=mid-1;
            }else {
                left=mid+1;
            }
        }
        return res;
    }
}